package org.lc.cmd.impl;

import org.lc.cmd.CmdRequest;
import org.lc.cmd.CmdSession;
import org.lc.cmd.CmdSysReq;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DefaultContextManager implements CmdContextSetter {
    public static final DefaultContextManager INSTANCE = new DefaultContextManager();

    private final Map<String, CmdSession> sessionMap = new ConcurrentHashMap<>();

    private final Map<String, CmdRequest> requestMap = new ConcurrentHashMap<>();

    private final Map<String, CmdSysReq> sysReqMap = new ConcurrentHashMap<>();

    @Override
    @SuppressWarnings("unchecked")
    public <T extends CmdSession> T getSessionById(String sessionId) {
        return (T) sessionMap.get(sessionId);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends CmdRequest> T getRequestById(String requestId) {
        return (T) requestMap.get(requestId);
    }

    @Override
    @SuppressWarnings("unchecked")
    public <T extends CmdSysReq> T getSysReqById(String sysReqId) {
        return (T) sysReqMap.get(sysReqId);
    }

    @Override
    public CmdSession putSession(CmdSession session) {
        return sessionMap.put(session.getId(), session);
    }

    @Override
    public CmdSession removeSession(String sessionId) {
        return sessionMap.remove(sessionId);
    }

    @Override
    public CmdRequest putRequest(CmdRequest cmdRequest) {
        return requestMap.put(cmdRequest.getId(), cmdRequest);
    }

    @Override
    public CmdRequest removeRequest(String requestId) {
        return requestMap.remove(requestId);
    }

    @Override
    public CmdSysReq putSysReq(CmdSysReq req) {
        return sysReqMap.put(req.getId(), req);
    }

    @Override
    public CmdSysReq removeSysReq(String sysReqId) {
        return sysReqMap.remove(sysReqId);
    }
}
